#!/bin/bash
set -e

_help () {
echo "Copyright (c) 2020 Luet Extensions

luet abuildspec [opts]

Converts a local aports to a Luet tree

Example:

  $> luet abuildspec /aports/path newcategory

"
return 0
}

if [ $# -eq 0 ] || [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ "$1" == "help" ]; then
    _help
    exit 1
fi

DIR=$1

CATEGORY=${2:-alpine}

BASE_LAYER_CATEGORY="${BASE_LAYER_CATEGORY:-development}"
BASE_LAYER_NAME="${BASE_LAYER_NAME:-toolchain}"

create_sub_package() {
    local NAME=${1%%:*}   
    
    local PARENT=$2
    local TREE=$3

    local APKBUILD=$4

    echo "Creating sub package $NAME from $PARENT"

    makedepends=
    pkgname=
    pkgver=
    pkgdesc=
    url=
    license=
    depends=
    subpackages=
    provides=

    . "$APKBUILD"

    package_dir=$TREE/$NAME
    mkdir -p $package_dir || true
    touch $package_dir/definition.yaml || true
    touch $package_dir/build.yaml || true

    # Add 1 to a collection
    yq w -i $package_dir/definition.yaml "name" "$NAME" 
    yq w -i $package_dir/definition.yaml "version" "$pkgver" --style double
    yq w -i $package_dir/definition.yaml "category" "$CATEGORY"
    yq w -i $package_dir/definition.yaml "description" "$pkgdesc"
    yq w -i $package_dir/definition.yaml "uri[0]" "$url"
    yq w -i $package_dir/definition.yaml "license" "$license"

u=0
for i in $provides; do
    if [ "$i" == "-dev" ]  || [ "$i" == "" ] || [ "$i" == "$pkgname" ]; then
         continue
    fi
    i=${i%%:*} 
    i=${i%%[<>=]*}

        local cp=${i#\!}
		if [ $cp != $i ]; then
        continue
        fi;


    yq w -i $package_dir/definition.yaml "provides[$u].category" "$CATEGORY"
    yq w -i $package_dir/definition.yaml "provides[$u].name" "${i}${NAME/${i}${PARENT/$i/}/}" --style double
    yq w -i $package_dir/definition.yaml "provides[$u].version" ">=0"
        u=$((u+1))

done

    cat << 'EOF' > $package_dir/build.sh
#!/bin/bash
cp -rf pkg/$PACKAGE_NAME/* / || true
EOF
    yq w -i $package_dir/build.yaml "requires[0].category" "$CATEGORY"
    yq w -i $package_dir/build.yaml "requires[0].name" "$PARENT"
    yq w -i $package_dir/build.yaml "requires[0].version" ">=0"
    yq w -i $package_dir/build.yaml "package_dir" "/luetbuild/pkg/{{.Values.name}}"
    yq w -i $package_dir/build.yaml "steps[0]" "sh build.sh"
}

convertapk() {

local APKBUILD=$1

makedepends=
pkgname=
pkgver=
pkgdesc=
url=
license=
depends=
subpackages=
provides=

. "$APKBUILD"

packagedir=$(dirname $APKBUILD)
name=$(basename $packagedir)
#pkgname=$name
touch $packagedir/definition.yaml || true

# Add 1 to a collection
yq w -i $packagedir/definition.yaml "name" "$pkgname" 
yq w -i $packagedir/definition.yaml "version" "$pkgver" --style double
yq w -i $packagedir/definition.yaml "category" "$CATEGORY"
yq w -i $packagedir/definition.yaml "description" "$pkgdesc"
yq w -i $packagedir/definition.yaml "uri[0]" "$url"
yq w -i $packagedir/definition.yaml "license" "$license"

readarray -t s <<<"$source"

yq w -i $packagedir/definition.yaml "labels.\"package.source\"" "${s[0]}" --style double
cat << 'EOF' > $packagedir/build.sh
#!/bin/sh
set -e

if [ -e "/build/env" ]; then
    source /build/env
fi
# export CBUILD=$MUSL_TARGET
# export LUET_YES=true
# luet install libs/libressl development/abuild vcs/git development/make
echo "#!/bin/sh" > /usr/bin/apk
echo "exit 0" >> /usr/bin/apk
echo "#!/bin/sh" > /usr/bin/abuild-sign
echo "touch \$2" >> /usr/bin/abuild-sign
echo "exit 0" >> /usr/bin/abuild-sign
chmod +x /usr/bin/apk
chmod +x /usr/bin/abuild-sign

echo "" | SUDO=" " abuild-keygen -a -i
export ABUILD_BOOTSTRAP=true # Disable package checks. other wise add !check to each single package options
mkdir -p /var/cache/distfiles
APK="" abuild -rFdK
cp -rf pkg/$PACKAGE_NAME/* / || true

EOF

touch $packagedir/build.yaml || true

# yq w -i $pkgname/build.yaml "image" "quay.io/mocaccino/micro-toolchain:latest"
# yq w -i $pkgname/build.yaml "env[0]" 'CFLAGS="-g -O2 -I/include -I/usr/include -I/usr/include/X11 -static"'
# yq w -i $pkgname/build.yaml "env[1]" 'CPPFLAGS="-I/include -I/usr/include -I/usr/include/X11 -static"'
# yq w -i $pkgname/build.yaml "env[2]" 'LUET_YES="true"'

# # Generate steps
# yq w -i $pkgname/build.yaml "prelude[0]" "luet install libs/zlib shells/bash utils/autoconf utils/curl libs/zlib vcs/git development/automake development/pkg-config development/m4 lang/python \
#    development/make system/musl-toolchain"
yq w -i $packagedir/build.yaml "package_dir" "/luetbuild/pkg/{{.Values.name}}"
yq w -i $packagedir/build.yaml "steps[0]" "sh build.sh"

# Generate depends
u=0
for i in $makedepends; do
#i="${i/-dev/}"  
    i=${i%%:*} 
i=${i%%[<>=]*}
  if [ "$i" == "" ] || [ "$i" == "$pkgname" ]  ; then
         continue
    fi
       local cp=${i#\!}
		if [ $cp != $i ]; then
        continue
        fi;
    yq w -i $packagedir/build.yaml "requires[$u].category" "$CATEGORY"
    yq w -i $packagedir/build.yaml "requires[$u].name" "$i"
    yq w -i $packagedir/build.yaml "requires[$u].version" ">=0"
    u=$((u+1))
done


    yq w -i $packagedir/build.yaml "requires[$u].category" "$BASE_LAYER_CATEGORY"
    yq w -i $packagedir/build.yaml "requires[$u].name" "$BASE_LAYER_NAME"
    yq w -i $packagedir/build.yaml "requires[$u].version" ">=0"

u=0
for i in $depends; do
    if [ "$i" == "-dev" ]  || [ "$i" == "" ] || [ "$i" == "$pkgname" ]; then
         continue
    fi
    i=${i%%:*} 
    i=${i%%[<>=]*}

        local cp=${i#\!}
		if [ $cp != $i ]; then
        continue
        fi;

    yq w -i $packagedir/definition.yaml "requires[$u].category" "$CATEGORY"
    yq w -i $packagedir/definition.yaml "requires[$u].name" "${i}" --style double
    yq w -i $packagedir/definition.yaml "requires[$u].version" ">=0"
    u=$((u+1))
done

## PRovides start
u=0
for i in $provides; do
    if [ "$i" == "-dev" ]  || [ "$i" == "" ] || [ "$i" == "$pkgname" ]; then
         continue
    fi
    i=${i%%:*} 
    i=${i%%[<>=]*}

        local cp=${i#\!}
		if [ $cp != $i ]; then
        continue
        fi;

    yq w -i $packagedir/definition.yaml "provides[$u].category" "$CATEGORY"
    yq w -i $packagedir/definition.yaml "provides[$u].name" "${i}" --style double
    yq w -i $packagedir/definition.yaml "provides[$u].version" ">=0"
    echo "Writing provide and creating build package"

    mkdir $packagedir/"${i}"  || true
    touch $packagedir/"${i}"/build.yaml || true
    touch $packagedir/"${i}"/definition.yaml  || true

    yq w -i $packagedir/"${i}"/definition.yaml "name" "$i" 
yq w -i $packagedir/"${i}"/definition.yaml "version" "0" --style double
yq w -i $packagedir/"${i}"/definition.yaml "category" "$CATEGORY"

    yq w -i $packagedir/"${i}"/build.yaml "requires[0].name" "$pkgname" 
yq w -i $packagedir/"${i}"/build.yaml "requires[0].version" ">=0" --style double
yq w -i $packagedir/"${i}"/build.yaml "requires[0].category" "$CATEGORY"
    u=$((u+1))
done
## Provides END

for i in $subpackages; do
  

        if [ "$i" == "-dev" ]  || [ "$i" == "" ] || [ "$i" == "$pkgname" ]; then
         continue
    fi
    i=${i%%:*} 
    i=${i%%[<>=]*}

        local cp=${i#\!}
		if [ $cp != $i ]; then
        continue
        fi;
    create_sub_package "$i" "$pkgname" "$packagedir" "$APKBUILD"
done

}


for i in $(find $DIR -type d -maxdepth 2)
do     
    if [ -e "$i"/APKBUILD ]; then
    echo "Converting $i"
    convertapk "$i"/APKBUILD
    fi
done
